import { ref } from "vue";
import { selectOutPut } from "../public/selectModel";

//公式匹配模型
export interface matchingData {

    id: string;
    // 父级id
    pid: string;
    //匹配组（and,or） 
    matchGroup: string;
    //层级
    level: number;
    //匹配条件
    matchingWhere: matchingWhereData[];
    //子集
    children: matchingData[];
}

//匹配条件模型
export interface matchingWhereData {
    //主键
    id: string;
    //字段key（选中的字段）
    fieldKey: string;
    //匹配数据key（选中的匹配值）
    matchDataKey: string;
    //匹配数据名称（选中的匹配值）
    matchDataName: string;
    //等式符号key（选中的符号）
    matchEquationKey: string;
    //显示数据控件
    showDataControl: showDataControlEnum;
    //显示控件中的数据源类型
    showControlDataSourceType: number;
    //绑定数据源--只有showDataControl=RadioSelect下拉框时才有数据
    bindDataSource: selectOutPut[];
}

//显示数据控件枚举
export enum showDataControlEnum {

    /// <summary>
    /// 输入框
    /// </summary>
    Input = 1,

    /// <summary>
    /// 数字输入框
    /// </summary>
    Number = 2,

    //时间
    DateTimes = 3,

    /// <summary>
    /// 单选下拉框
    /// </summary>
    RadioSelect = 4,

    /// <summary>
    /// 弹出层
    /// </summary>
    RadioLayer = 5,

    /// <summary>
    /// 多选弹出层
    /// </summary>
    MultipleLayer = 6,
}

//显示控件结果数据源枚举
export enum showControlDataSourceEnum {
    /// <summary>
    /// 手填
    /// </summary>
    HandInput = 1,

    /// <summary>
    /// 用户数据源
    /// </summary>
    Personnel = 2,

    /// <summary>
    /// 角色数据源
    /// </summary>
    UserRole = 3,

    /// <summary>
    /// 部门数据源
    /// </summary>
    Department = 4,

    /// <summary>
    /// 公司数据源
    /// </summary>
    Corporation = 5,

    /// <summary>
    /// 系统数据源
    /// </summary>
    System = 6,

    /// <summary>
    /// 菜单数据源
    /// </summary>
    Menu = 7,

    /// <summary>
    /// 布尔类型
    /// </summary>
    Boole = 8,

    /// <summary>
    /// 数据状态
    /// </summary>
    DataState = 9,

}

//匹配数据下拉框输出模型
export interface matchingSelectExtendOutPut {
    //显示值
    label: string

    //key值
    value: string

    //是否禁用
    disabled: boolean

    //显示数据控制
    showDataControl: showDataControlEnum,

    //显示控件数据源类型
    showControlDataSource: showControlDataSourceEnum,

    //等式符号集合
    matchEquationOptions: selectOutPut[]
}

//组条件
export const matchingGroup = [
    {
        label: '且',
        value: 'And',
        disabled: false
    },
    {
        label: '或',
        value: 'Or',
        disabled: false
    }
]

//匹配返回模型
export interface matchResultData {
    isSuccess: boolean;
    msg: string;
    ruleExpression: AnalysisRuleDataOutPut;
    matchRuleJson: string;
}

//解析规则数据输出模型
export interface AnalysisRuleDataOutPut {
    //英文匹配规则
    analysisRule: string;
    //中文匹配规则
    analysisRule_CN: string;
}


//选择类型
export enum SelectTypeEnum {
    //单选
    Radio = 1,
    //多选
    Multiple = 2,
}

/* 生成随机不重复id */
export const randamId = function () {
    let n = 1;
    let arr = [];
    for (let i = 0; i < n; i++) {
        arr[i] = parseInt((Math.random() * 10000000000).toString());
    }
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            if (arr[i] === arr[j]) {
                randamId();
                return false;
            }
        }
    }
    return ("Item-" + arr).toString();
};

/* 验证是否有空值 */
const isTrue = ref<boolean>(false);
export function verificationEmpty(data: matchingData[]): boolean {
    data.forEach(element => {
        element.matchingWhere.forEach(w => {
            if (w.matchEquationKey.length == 0 || w.matchDataKey.length == 0 || w.fieldKey.length == 0) {
                isTrue.value = true;
            }
            else {
                isTrue.value = false;
            }
        });
        if (element.children.length > 0) {
            verificationEmpty(element.children);
        }
    });
    return isTrue.value;
}
